package com.scau.dao.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.scau.dao.BaseDao;
import com.scau.dao.ISaleOrderItemDao;
import com.scau.vo.Product;
import com.scau.vo.SaleOrder;
import com.scau.vo.SaleOrderItem;
import com.scau.vo.Service;

/**
 * @author Jinglun
 * @create 2017年4月22日
 */
public class SaleOrderItemDaoImpl extends BaseDao implements ISaleOrderItemDao {

	private Session session = null;
	private Transaction tx = null;
	private String hql = "";
	
	@Override
	public boolean saveSerSaleOrderItem(SaleOrderItem saleOrderItem) {
		try {
			session = getSession();
			tx = session.beginTransaction();
			
			HttpServletRequest request = ServletActionContext.getRequest();
			
			String id1 = request.getParameter("saleOrderItem.itemId");
			Integer itemId = Integer.parseInt(id1);
			
			System.out.println("itemId:"+itemId);
						
			String saleOrderNo = request.getParameter("saleOrderItem.saleOrderNo");
			
			System.out.println("saleOrderNo:"+saleOrderNo);
			
			String sql = "insert into sale_order_item"
					+ "(saleOrderNo, itemId, quantity, salePrice, subTotal) "
					+ "values" + "('" + saleOrderNo + "'," + itemId + ",'"
					+ saleOrderItem.getQuantity() + "','"
					+ saleOrderItem.getSalePrice() + "','"
					+ saleOrderItem.getSubTotal() + "')";
			
			System.out.println(sql);
			
			Query query56 = session.createSQLQuery(sql);

			int a = query56.executeUpdate();
			if (a > 0) {
				System.out.println("success");
			} else {
				System.out.println("fail");
					}

			tx.commit();
			session.close();
			System.out.println("明细插入成功");
			return true;
		} catch (Exception e) {
			tx.commit();
			session.close();
			return false;
		}
	}
	
	@Override
	public boolean saveProSaleOrderItem(SaleOrderItem saleOrderItem) {
		
			session = getSession();
			tx = session.beginTransaction();
			
			HttpServletRequest request = ServletActionContext.getRequest();
			
			String id1 = request.getParameter("saleOrderItem.productId");
			Integer productId = Integer.parseInt(id1);
			
			System.out.println("依赖产品ID："+productId);
			
			System.out.println("添加产品");
			
			String saleOrderNo = request.getParameter("saleOrderItem.saleOrderNo");

			String sqlPro = "insert into sale_order_item"
					+ "(saleOrderNo, productId, quantity, salePrice, subTotal) "
					+ "values" + "('" + saleOrderNo + "'," + productId + ",'"
					+ saleOrderItem.getQuantity() + "','"
					+ saleOrderItem.getSalePrice() + "','"
					+ saleOrderItem.getSubTotal() + "')";
			
			System.out.println(sqlPro);

			Query query66 = session.createSQLQuery(sqlPro);
			
			int b = query66.executeUpdate();
			if (b > 0) {
				System.out.println("success");
			} else {
				System.out.println("fail");
			}
			
			tx.commit();
			session.close();
			System.out.println("明细插入成功产品");
			return true;
		
	}

	@Override
	public List<SaleOrderItem> listSaleOrderItem() {
		// TODO Auto-generated method stub
		return null;
	}
	
	/*
	 * 根据订单号查询订单
	 * 
	 * (non-Javadoc)
	 * 
	 * @see com.scau.dao.ISaleOrderItemDao#listDebtsItem()
	 */
	@Override
	public List<SaleOrderItem> getDebtsItem(String saleOrderNo) {
		session = getSession();
		System.out.println("订单号为" + saleOrderNo);
		hql = "from SaleOrderItem s where s.saleOrderNo = '" + saleOrderNo + "'";
		Query query = session.createQuery(hql);
		List<SaleOrderItem> list = query.list();
		System.out.println("根据订单号查出：" + list);
		session.close();
		return list;
	}

}
